package edu.npu.GraphIndex;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;


//初始化histogram以及遍历
public class HistogramInit implements Traversable {
	public List<Long> traversed = null;
	public Map<Long, String> label = null;
	public Queue<Long> queue = null;
	public Map<Integer, Map<String, CntVid>> histogram = null;
	
	public HistogramInit() {
		this.label = new HashMap<Long, String>();
		this.queue = new LinkedList<Long>();
		this.traversed = new ArrayList<Long>();
	}
	
	@Override
	public boolean isTraversed(long node) {
		if(traversed.contains(node))
			return true;
		return false;
	}
	@Override
	public void action(long node, int level) {
		int distance = level + 1;
		//将没遍历的节点加入histogram
		if(!histogram.containsKey(distance)) {
			histogram.put(distance, new HashMap<String, CntVid>());
		}
		Map<String, CntVid> lblCnt = histogram.get(distance);
		if(!lblCnt.containsKey(label.get(node))) {
			lblCnt.put(label.get(node), new CntVid());
		}
		CntVid cv = lblCnt.get(label.get(node));
		cv.add(node);
		//标记该节点已经被遍历
		traversed.add(node);
	}
	@Override
	public boolean isDone(int level) {
		//结束条件是是否达到了制定的hop值,或者
		//队列已经为空
		if(level + 1 > 3 || queue.isEmpty())
			return true;
		return false;
	}
	//清空histogram以便复用
	public void clear() {
		queue.clear();
		traversed.clear();
	}
}
